package com.sinosoft.perfect.system._unit.service;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.system._.po.TUnit;
import com.sinosoft.perfect.system._.po.TUnitInformation;
import com.sinosoft.perfect.system._.po.TUserRegisterNum;
import com.sinosoft.perfect.system._.service.ISortNumberService;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._.vo.SortNumber;
import com.sinosoft.perfect.system._unit.dao.IUnitDAO;
import com.sinosoft.perfect.system._unit.formBean.UnitForm;

public class UnitServiceImpl implements IUnitService {

	private IUnitDAO unitDAO;

	private ISortNumberService sortNumberService;

	public void setUnitDAO(IUnitDAO unitDAO) {
		this.unitDAO = unitDAO;
	}

	public void setSortNumberService(ISortNumberService sortNumberService) {
		this.sortNumberService = sortNumberService;
	}

	public String insertTUnit(TUnit tunit) throws SQLException {
		int sortNum = this.selectTUnitMAXSortNum(tunit) + 1;
		String id = Producer.generateIdOfTen();
		tunit.setUnitId(id);
		if (tunit.getSuperUnitId() == null || "".equals(tunit.getSuperUnitId()))
			tunit.setSuperUnitId("0");
		tunit.setUnitSortNumber(new Long(sortNum));
		tunit.setCreateUserId("1");
		tunit.setUnitType("1");
		tunit.setStatus("1");
		unitDAO.insertTUnit(tunit);

		TUnitInformation tunitinformation = new TUnitInformation();
		tunitinformation.setUnitId(id);
		this.insertTUnitInformation(tunitinformation);

		return id;
	}

	public void insertTUnitInformation(TUnitInformation tunitinformation) throws SQLException {
		String id = Producer.generateIdOfTen();
		tunitinformation.setId(id);
		unitDAO.insertTUnitInformation(tunitinformation);
	}

	public void insertTUserRegisterNum(TUserRegisterNum tuserregisternum) throws SQLException {
		String id = Producer.generateIdOfTen();
		tuserregisternum.setRegisterNumId(id);
		tuserregisternum.setStatus("1");
		tuserregisternum.setCreateUserId("1");
		unitDAO.insertTUserRegisterNum(tuserregisternum);
	}

	public List<UnitForm> selectTUnit(TUnit tunit) throws SQLException {
		return unitDAO.selectTUnit(tunit);
	}

	public int hasTUnitName(TUnit tunit) throws SQLException {
		int result = 0;
		Object obj = unitDAO.selectTUnitByName(tunit);
		if (obj != null)
			result = 1;
		return result;
	}

	public int hasTUnitUserRegisterNum(TUserRegisterNum tuserregisternum) throws SQLException {
		int result = 0;
		List<TUserRegisterNum> unums = unitDAO.selectTUserRegisterNumByNum(tuserregisternum);
		if (unums != null && unums.size() != 0)
			result = 1;
		return result;
	}

	public String[] selectTUnitAndChildIds(String unitId) throws SQLException {
		List<TUnit> tUnits = unitDAO.selectTUnitAndChildren(unitId);
		String[] unitIds = null;
		if (tUnits != null && tUnits.size() != 0) {
			int unitsLength = tUnits.size();
			unitIds = new String[unitsLength];
			for (int i = 0; i < unitsLength; i++) {
				unitIds[i] = tUnits.get(i).getUnitId();
			}
		}
		return unitIds;
	}

	public Long selectTUnitSortNum(TUnit tunit) throws SQLException {
		Long sortNum = null;
		Object obj = unitDAO.selectTUnitSortNum(tunit);
		if (obj != null)
			sortNum = Long.parseLong(obj.toString());
		return sortNum;
	}

	public Integer selectTUnitMAXSortNum(TUnit tunit) throws SQLException {
		Integer sortNum = 0;
		Object numObj = unitDAO.selectTUnitMAXSortNum(tunit);
		if (numObj != null)
			sortNum = Integer.parseInt(numObj.toString());
		return sortNum;
	}

	public List<TUnitInformation> selectTUnitInfo(TUnitInformation tunitinfo) throws SQLException {
		return unitDAO.selectTUnitInfo(tunitinfo);
	}

	public List<TUserRegisterNum> selectTUserRegisterNum(TUserRegisterNum tuserregisternum) throws SQLException {
		return unitDAO.selectTUserRegisterNum(tuserregisternum);
	}

	public void updateTUnitByPK(TUnit tunit) throws SQLException {
		unitDAO.updateTUnitByPK(tunit);
	}

	public String[] updateTUnitForStatusByPK(TUnit tunit) throws SQLException {
		unitDAO.updateTUnitStatusByPK(tunit);
		return selectTUnitAndChildIds(tunit.getUnitId());
	}

	public void updateTUnitInfoByPK(TUnitInformation tunitinfo) throws SQLException {
		unitDAO.updateTUnitInfoByPK(tunitinfo);
	}

	public void updateTUserRegisterNumByPK(TUserRegisterNum tuserregisternum) throws SQLException {
		unitDAO.updateTUserRegisterNumByPK(tuserregisternum);
	}

	public void updateTUserRegisterNumTimesByUnitId(String unitId) throws SQLException {
		unitDAO.updateTUserRegisterNumTimesByUnitId(unitId);
	}

	public void deleteTUnit(String unitId) throws SQLException {
		unitDAO.deleteTUserUnitRelation(unitId);
		unitDAO.deleteTUserRegisterNumByUnitId(unitId);
		unitDAO.deleteTUnitInformation(unitId);
		unitDAO.deleteTUnits(unitId);
	}

	public void deleteTUnits(String[] unitIds) throws SQLException {
		for (int i = 0; i < unitIds.length; i++) {
			String unitId = unitIds[i];
			this.deleteTUnit(unitId);
		}
	}

	public void deleteTUserRegisterNumByUnitId(String unitId) throws SQLException {
		unitDAO.deleteTUserRegisterNumByUnitId(unitId);
	}

	public boolean sortUnit(long newSortNumber, String unitId) throws SQLException {
		SortNumber sortNumber = new SortNumber(newSortNumber, unitId, "T_UNIT", "UNIT_ID", "SUPER_UNIT_ID", "UNIT_SORT_NUMBER");
		sortNumberService.sortNumber(sortNumber);
		return false;
	}

	@Override
	public List<TUnit> selectTUnitByUnitIds(String unitIds) throws SQLException {
		return unitDAO.selectTUnitByUnitIds(unitIds);
	}

}
